/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;

/**
 *
 */
public class UserAlert
{
  /** Fields */
  private static String alertTitle;
  private static String ok;
  private static String yes;
  private static String no;

  public enum Result
  {
    UNKNOWN,
    SUCCESS,

    DB_ACCESS_ERROR,
    DATA_INSERT_ERROR,
    DATA_UPDATE_ERROR,
    DATA_DELETE_ERROR,
    DATA_FIND_ERROR,
    CREATE_TABLE_ERROR,
    TABLE_NOT_EXISTS,
    ENTER_ALL_DATA,
    TABLE_ACCESS_ERROR,
    DATA_VALUES_ERROR,
    MORE_DATA_REQUIRED,
    WRONG_VALUES,

    LON_OUT_OF_RANGE,
    LAT_OUT_OF_RANGE,

    CLOSE_TRACK_ERROR,
    RESUME_RECORDING_ERROR,

    NAME_TOO_LONG,
    DESC_TOO_LONG,

    SERVER_REJECTED_DATA,
  };

  /**
   * Initializes the common dialog labels.
   *
   * @param context
   *          The application context.
   */
  private static void init(Context context)
  {
    UserAlert.alertTitle = context.getResources().getString(R.string.titleAlert);
    UserAlert.ok = context.getResources().getString(R.string.alert_dialog_ok);
    UserAlert.yes = context.getResources().getString(R.string.alert_dialog_yes);
    UserAlert.no = context.getResources().getString(R.string.alert_dialog_no);
  }

  /**
   * Displays the question dialog box.
   *
   * @param context
   *          The application context.
   * @param localizeId
   *          The string id.
   * @param onButtonClicked
   *          The button listener.
   */
  public static void question(Context context, int localizeId,
      DialogInterface.OnClickListener onButtonClicked)
  {
    final String message = context.getString(localizeId);
    question(context, message, onButtonClicked);
  }

  /**
   * Displays the question dialog box.
   *
   * @param context
   *          The application context.
   * @param text
   *          The question string.
   * @param onButtonClicked
   *          The button listener.
   */
  public static void question(Context context, String text,
      DialogInterface.OnClickListener onButtonClicked)
  {
    init(context);

    AlertDialog alert = new AlertDialog.Builder(context).create();
    alert.setTitle(alertTitle);
    alert.setMessage(text);
    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.setButton(AlertDialog.BUTTON_POSITIVE, UserAlert.yes, onButtonClicked);
    alert.setButton(AlertDialog.BUTTON_NEGATIVE, UserAlert.no,
        (DialogInterface.OnClickListener) null);
    alert.show();
  }

  /**
   * Displays the information dialog.
   *
   * @param context
   *          The application context.
   * @param text
   *          The information string.
   */
  public static void show(Context context, String text)
  {
    init(context);

    AlertDialog alert = new AlertDialog.Builder(context).create();
    alert.setTitle(alertTitle);
    alert.setMessage(text);
    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.setButton(AlertDialog.BUTTON_POSITIVE, ok, (Message)null);
    alert.show();
  }

  /**
   * Displays any result information.
   *
   * @param context
   *          The application context.
   * @param result
   *          The result object.
   */
  public static void show(Context context, Result result)
  {
    final String msg = getDescription(context, result);
    show(context, msg);
  }

  /**
   * Displays the information dialog with a specific button listener.
   *
   * @param context
   *          The application context.
   * @param text
   *          The information string.
   * @param onButtonClicked
   *          The button listener.
   */
  public static void show(Context context, String text,
      DialogInterface.OnClickListener onButtonClicked)
  {
    init(context);

    AlertDialog alert = new AlertDialog.Builder(context).create();
    alert.setTitle(alertTitle);
    alert.setMessage(text);
    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.setButton(AlertDialog.BUTTON_POSITIVE, ok, onButtonClicked);
    alert.show();
  }

  /**
   * Displays the information dialog with a specific button listener and an
   * exit listener.
   *
   * @param context
   *          The application context.
   * @param text
   *          The information string.
   * @param onButtonClicked
   *          The button listener.
   * @param onDismiss
   *          The exit listener.
   */
  public static void show(Context context, String text,
      DialogInterface.OnClickListener onButtonClicked,
      DialogInterface.OnDismissListener onDismiss)
  {
    init(context);

    AlertDialog alert = new AlertDialog.Builder(context).create();
    alert.setTitle(alertTitle);
    alert.setMessage(text);
    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.setButton(AlertDialog.BUTTON_POSITIVE, ok, onButtonClicked);
    alert.setOnDismissListener(onDismiss);
    alert.show();
  }

  /** Gets description */
  private static String getDescription(Context context, Result result)
  {
    switch (result)
    {
      case SUCCESS: return context.getString(R.string.alert_msg_ok);

      case DB_ACCESS_ERROR: return context.getString(R.string.alert_msg_db_access_error);
      case DATA_INSERT_ERROR: return context.getString(R.string.alert_msg_data_insert_error);
      case DATA_UPDATE_ERROR: return context.getString(R.string.alert_msg_data_update_error);
      case DATA_DELETE_ERROR: return context.getString(R.string.alert_msg_data_delete_error);
      case CREATE_TABLE_ERROR: return context.getString(R.string.alert_msg_create_table_error);
      case TABLE_NOT_EXISTS: return context.getString(R.string.alert_msg_table_not_exists);
      case ENTER_ALL_DATA: return context.getString(R.string.alert_msg_enter_all_data);
      case TABLE_ACCESS_ERROR: return context.getString(R.string.alert_msg_table_access_error);
      case DATA_VALUES_ERROR: return context.getString(R.string.alert_msg_data_values_error);
      case MORE_DATA_REQUIRED: return context.getString(R.string.alert_msg_more_data_required);
      case WRONG_VALUES: return context.getString(R.string.alert_msg_wrong_values);
      case DATA_FIND_ERROR: return context.getString(R.string.alert_msg_data_find_error);

      case LON_OUT_OF_RANGE: return context.getString(R.string.alert_msg_lon_out);
      case LAT_OUT_OF_RANGE: return context.getString(R.string.alert_msg_lat_out);

      case CLOSE_TRACK_ERROR: return context.getString(R.string.alert_msg_close_track_error);
      case RESUME_RECORDING_ERROR: return context.getString(R.string.alert_msg_resume_recording_error);

      case NAME_TOO_LONG: return context.getString(R.string.alert_msg_name_too_long);
      case DESC_TOO_LONG: return context.getString(R.string.alert_msg_desc_too_long);

      case SERVER_REJECTED_DATA: return context.getString(R.string.alert_msg_server_rejected_data);
    }
    return "";
  }
}